package com.hx.voice.recognition.auth.server.interceptor;

import com.hx.voice.recognition.auth.server.configuration.ClientConfiguration;
import com.hx.voice.recognition.auth.server.service.IAuthClientService;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @descripe：客户端token拦截器
 * @company：南京华讯方舟通讯设备有限公司
 * @author：zhoujinbing
 * @time：2019/6/14 10:36
 * @version：V1.0
 */
@Slf4j
public class ClientTokenInterceptor implements RequestInterceptor {

    @Autowired
    private ClientConfiguration clientConfiguration;

    @Autowired
    private IAuthClientService authClientService;


    @Override
    public void apply(RequestTemplate template) {
        try {
            log.info("请求客户端ID:{},生成的应用token为:{}",clientConfiguration.getClientId(),authClientService.apply(clientConfiguration.getClientId(),clientConfiguration.getClientSecret()));
            template.header(clientConfiguration.getClientTokenHeader(),authClientService.apply(clientConfiguration.getClientId(),clientConfiguration.getClientSecret()));
        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}